package com.umpay.util;

import com.umpay.dict.XmlData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * ***********************************************************************
 * <br>description : 请求业务网关加密处理帮助类
 * @author      xiatian
 * @date        Oct 15, 2012 5:35:25 PM
 * @version     1.0  
 ************************************************************************
 */
public class HashSignHelper {

	private static final Logger logger = LogManager.getLogger(HashSignHelper.class);

	private static final String ALG_MD5 = "MD5";
	private static final String ALG_SHA1 = "SHA-1";
    
	/**
	 * 
	 * <br>description :验证加密是否正确
	 * @param reqSign
	 * @param params
	 * @param key
	 * @param signType
	 * @param inputCharset
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:32:21 PM
	 */
	public static boolean verifySign(String reqSign, Map<String, Object> params, String key, String signType, String inputCharset) {
		if (StringUtils.isEmpty(reqSign))
			return false;

		String sign = getHashSignMsg(params, key, signType, inputCharset);
		return reqSign.equalsIgnoreCase(sign);
	}

	/**
	 * 
	 * <br>description : 验证加密是否正确
	 * @param reqSign
	 * @param params
	 * @param key
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:32:32 PM
	 */
	public static boolean verifySign(String reqSign, Map<String, Object> params, String key) {
		return verifySign(reqSign, params, key, ALG_SHA1, XmlData.GBK);
	}

	/**
	 * 
	 * <br>description : 获取加密后数据
	 * @param params
	 * @param key
	 * @param signType
	 * @param inputCharset
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:32:50 PM
	 */
	public static String getHashSignMsg(Map<String, Object> params, String key, String signType, String inputCharset) {
		String signMsg = "";
		if (ALG_MD5.equals(signType)) {
			signMsg = getMd5SignMsg(params, key, inputCharset);
		} else if (ALG_SHA1.equals(signType)) {
			signMsg = getSha1SignMsg(params, key, inputCharset);
		} else {
			signMsg = getSha1SignMsg(params, key, inputCharset);
		}
		return signMsg;
	}

	/**
	 * 
	 * <br>description : 获取MD5加密数据
	 * @param params
	 * @param key
	 * @param inputCharset
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:33:14 PM
	 */
	public static String getMd5SignMsg(Map<String, Object> params, String key, String inputCharset) {
		String signSource = getSignSourMsg(params, key);
		String signMsg = HashEncrypt.doEncrypt(signSource, ALG_MD5, inputCharset);
		return signMsg;
	}

	/**
	 * 
	 * <br>description : 获取SHA1加密数据
	 * @param params
	 * @param key
	 * @param inputCharset
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:33:20 PM
	 * @see         TODO
	 */
	public static String getSha1SignMsg(Map<String, Object> params, String key, String inputCharset) {
		String signSource = getSignSourMsg(params, key);
		logger.info(String.format("签名源数据为：%s" ,signSource));
		String signMsg = HashEncrypt.doEncrypt(signSource, ALG_SHA1, inputCharset);
		logger.info(String.format("签名:%s" ,signMsg));
		return signMsg;
	}

	/**
	 * 
	 * <br>description : 获取明文和密文的完整组合数据
	 * @param params
	 * @param key
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:33:26 PM
	 */
	private static String getSignSourMsg(Map<String, Object> params, String key) {
//		removeExcludeFileds(params);
//		decodeUrlEncodedFields(params);
		String signSource = createLinkString(params);
		if (key != null && key.length() > 0) {
			signSource = signSource + "&signKey=" + key;
		}
		return signSource;

	}
	
	/**
	 * <br>description : 获取明文串
	 * @param params
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 15, 20125:33:39 PM
	 */
	private static String createLinkString(Map<String, Object> params) {
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);

		StringBuffer prestr = new StringBuffer("");

		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = (String) params.get(key);
			if (value != null && value.length() > 0) {
				prestr.append(key).append("=").append(value).append("&");
			}
		}
		return prestr.deleteCharAt(prestr.toString().length()-1).toString();
	}



}
